// https://www.lintcode.com/problem/anagrams/description

class Solution {
public:
    /**
     * @param strs: A list of strings
     * @return: A list of strings
     */
    vector<string> anagrams(vector<string> &strs) {
        map<string, vector<string>> m;
        vector<string> result;
        for (string s: strs) 
        {
            string tmp = s;
            sort(tmp.begin(), tmp.end());
            m[tmp].push_back(s);
            if (m[tmp].size() > 1) 
            {
                if (m[tmp].size() == 2) result.insert(result.end(), m[tmp].begin(), m[tmp].end());
                else result.push_back(s);
            }
        }
        return result;
    }
};